<?php namespace testsacvoyage\storage;

require_once 'AbstractTest.php';

require_once 'sacvoyage/storage/Writer.php';
use \sacvoyage\storage\Writer;

require_once 'sacvoyage/storage/Table.php';
use \sacvoyage\storage\Table;

require_once 'sacvoyage/Pathfinder.php';
use \sacvoyage\Pathfinder;

require_once 'sacvoyage/storage/util.php';
use \sacvoyage\storage;

class WriterTest extends AbstractTest {

	// TODO: check if iterator is empty

	public function testGo() {
		$this->resetDatabase();
		$this->getGateway()->query("CREATE TABLE WriterTestTable (
	_id       TEXT NOT NULL, 
	_rev      TEXT NOT NULL,
	path	    TEXT NOT NULL,
	type      SET('string', 'number', 'object', 'array', 'boolean', 'null', 'missing') NOT NULL,
	value MEDIUMTEXT, -- up to 8388608
	INDEX(_id(16)), INDEX(_rev(20)), INDEX(path(25)), INDEX(type), INDEX(value(25))
) DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

		$table=new Table($this->getGateway(), 'WriterTestTable');
		$writer=new Writer($table, $this->getTestDocumentSet());
		$writer->go();

		$expected=array();
		foreach($this->getTestDocumentSet() as $document) {
			$finder=new Pathfinder($document);
			foreach($finder as $path=>$value) {

				$type=storage\json_typeof($value);

				$expected[]=array(
					'_id'=>$finder['._id'], 
					'_rev'=>$finder['._rev'], 
					'path'=>$path,
					'type'=>$type,
					'value'=>$value
				);
			}
		}

		$result=$this->getGateway()->query("SELECT _id, _rev, path, type, value FROM WriterTestTable");
		$written=array();
		while($row=$result->fetch_assoc()) {
			$written[]=$row;
		}

		$this->assertEquals($expected, $written);

	}


}

?>
